import deviceApi from "../../api/deviceInfo";
import { toast, hasLogin } from "../../utils/util";
import storage from '../../utils/storage';
Page({
  data: {
    statusBarHeight: getApp().globalData.statusBarHeight,
    tabsList: [],
    tabsList2: [
      {
        label: '全部',
        id: 0
      }
    ],
    allList: [],
    visible: false,
    style: 'border: 1rpx solid #008b28;border-radius: 12rpx;padding: 20rpx',
    popupStyle: 'padding-bottom: 0',
    pageSize: 10,
    pageNum: 1,
    status: 'more',
    type: 1,
    deviceName: '',
    listIndex: null,
    defaultValue: 0,
    tabsVal: '',
    sort: 'asc',
    bgColor: 'transparent'
  },
  onLoad (options) {
    if(!hasLogin()) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  onShow() {
    if(hasLogin()) {
      this.setData({
        tabsList2: [
          {
            label: '全部',
            id: 0
          }
        ]
      })
      this.getGroup();
      this.getDeviceCount();
    }
  },
  init () {
    let params = {
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum,
      onLine: this.data.tabsVal,
      groupId: this.data.defaultValue?this.data.defaultValue: '',
      isAsc: this.data.sort,
      orderByColumn: 'di.name'
    }
    deviceApi.getUserDeviceList(params).then(res => {
      this.data.allList = this.data.allList.concat(res.rows)
      this.data.pageNum++
      this.setData({
        allList: this.data.allList,
        pageNum: this.data.pageNum
      })
      if(res.rows.length < this.data.pageSize){
        this.setData({
          status: 'noMore',
        })
      }
    })
  },
  getDeviceCount () {
    this.reset();
    deviceApi.getDeviceStatusCount().then(res => {
      res.data.forEach(async item => {
        if (item.onLine === null) {
          item.onLine = '';
        }
        item.label = await this.getTabsLabel(item.onLine);
      })
      setTimeout(() => {
        this.setData({
          tabsList: res.data
        })
        this.init();
      },300)
      
    })
  },
  getTabsLabel (data) {
    let str = null;
    switch (data) {
      case '':
        str = '全部'
        break;
      case 0:
        str = '离线'
        break;
      case 1:
        str = '在线'
        break;
      case 2:
        str = '故障'
        break;
    }
    return str
  },
  handleChange(e) {
    let { index } = e.currentTarget.dataset
    this.data.allList[index].deviceInfoVo.onLine = e.detail.value ? 1 : 0;
    this.setData({
      allList: this.data.allList,
    });
  },
  // 批量操作
  jumpBatch () {
    wx.navigateTo({
      url: '/pages/bulkOperation/index',
    })
  },
  editGroup (e) {
    let { index } = e.currentTarget.dataset;
    this.setData({
      visible: true,
      listIndex: index,
      deviceName: this.data.allList[index].name
    })
    this.init();
  },
  onVisibleChange(e) {
    this.setData({
      visible: e.detail.visible,
    });
  },
  changeTap (e) {
    this.setData({
      deviceName: e.detail.value
    })
  },
  onTabsChange (e) {
    this.setData({
      tabsVal: e.detail.value,
      allList: [],
      pageNum: 1
    })
    this.init()
  },
  onTabsChange1 (e) {
    this.setData({
      defaultValue: e.detail.value,
      allList: [],
      pageNum: 1
    })
    this.init()
  },
  
  getGroup () {
    let params = {
      pageSize: 1,
      pageNum: 100
    }
    deviceApi.getDeviceGroupList(params).then(res => {
      res.rows.forEach(item => {
        item.label = item.name;
        item.value = item.id;
      })
      this.data.tabsList2 = this.data.tabsList2.concat(res.rows)
      this.setData({
        tabsList2: this.data.tabsList2
      })
    })
  },
  submit () {
    let params = {
      id: this.data.allList[this.data.listIndex].id,
      name: this.data.deviceName,
      deviceId: this.data.allList[this.data.listIndex].deviceId,
      groupId: this.data.allList[this.data.listIndex].groupId,
      onLine: this.data.allList[this.data.listIndex].onLine
    }
    deviceApi.editDevice(params).then(res => {
      toast(res.msg);
      this.reset();
      this.init();
    })
  },

  reset () {
    this.setData({
      pageNum: 1,
      allList: [],
      visible: false,
    })
  },

  jumpDetail (e) {
    let { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/setDetail/index?id=${id}`
    })
  },
  changeAsc () {
    this.reset();
    if (this.data.sort === 'asc') {
      this.setData({
        sort: 'desc'
      })
    } else {
      this.setData({
        sort: 'asc'
      })
    }
    this.init();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.status = 'noMore') {
      return
    }
    this.init();
  },
  onPageScroll: function (e) {
    if (Number(e.scrollTop) > 30) {
      this.setData({
        bgColor: "#fff"
      })
    } else {
      this.setData({
        bgColor: "transparent"
      })
    }
  }
})
